﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.MVC.Classes;
using KPIS.GERP.SERVICE.Model;

namespace KPIS.GERP.MVC.Controllers
{
    public class ApprovalTaskController : Controller
    {
        public SsMng SsMng = new SsMng();
        public static Guid DaemonWorkflowUniqueId;

        public ActionResult Index()
        {
            List<approval_task> obj;
            using (var serviceClient = new ApprovalServiceWcfIisRef.AbsenceApprovalWCFClient())
            {
                obj = (from t in serviceClient.GetTasks()
                       select t).OrderByDescending(t => t.ApprovalTaskSeq).ToList();
            }
            return View(obj);
        }

        public ActionResult TakeAction(Guid wfInstantId, string actionCode, string messageTitle, string messageReasons, string backto = "")
        {
            try
            {
                using (var serviceClient = new ApprovalServiceIisRef.ServiceClient())
                {
                    serviceClient.ApproveAbsence(wfInstantId,
                                                 ApprovalUtil.TaskActionFromString(actionCode),
                                                 SsMng.EmpSeqI, messageReasons, messageTitle);
                }
            } catch (Exception ex)
            {
                throw ex;
            }
            if (backto == "Absence")
                return RedirectToAction("Index", "Absence");

            if (backto == "SalaryPromote")
                return RedirectToAction("Index", "SalaryPromote");

                return RedirectToAction("Index"); 
        }

        public ActionResult ViewDetails(int approvalTaskSeq)
        {
            using (var serviceClient = new ApprovalServiceWcfIisRef.AbsenceApprovalWCFClient())
            {
                var obj = (from t in serviceClient.GetTasks()
                           where t.ApprovalTaskSeq == approvalTaskSeq
                           select t).OrderByDescending(t => t.ApprovalTaskSeq).FirstOrDefault();

                return View("ViewDetails", obj);
            }
        }

        public ActionResult RuleApproveList()
        {
            var ruleApproveDal = new KPIS.GERP.SERVICE.DAL.RuleApprove();
            var employeeDal = new KPIS.GERP.SERVICE.DAL.Employee();
            ViewBag.emp = employeeDal.GetHrmPsEmployeeLite();
            ViewBag.user = employeeDal.GetUsers();
            ViewBag.ruleApproveGroups = ruleApproveDal.GetRuleApproveGroup(null);
            ViewBag.ruleApproveTypes = ruleApproveDal.GetRuleApproveType(null, null, false);
            var ruleApproves = ruleApproveDal.GetRuleApprove("", "", true, false, false);
            return View(ruleApproves);
        } 
    }
}
